package gamagora.game.states
{
	import gamagora.game.entity.OSBird;
	import gamagora.game.entity.OSBlurEffect;
	import gamagora.game.entity.OSMonkey;
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	import gamagora.game.utils.CreditsList;
	import gamagora.game.utils.OSBirdTrio;
	import gamagora.game.world.maps.OSMap;
	
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.FlxSound;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	
	public class OSEndWomanState extends FlxState
	{
		private static const SPEED:Number = 75.0;
		private static const IDLE:String = "idle";
		private static const MOVE:String = "move";
		private static const TAKE_SARI:String = "take_sari";
		
		private var _blackFade:FlxSprite;
		private var _completeFade:Boolean;
		private var _woman:FlxSprite;
		private var _monkey:OSMonkey;
		private var _goHome:Boolean;
		private var _creditList:CreditsList;
		private var _goToMenu:Boolean;
		private var _cutScene:Boolean;
		private var _birdTrio:OSBirdTrio;
		
		private var _music:FlxSound;
		
		public override function create():void
		{
			add(new FlxSprite(0, 0, OSImages.BG));
			add(new FlxSprite(0, 0, OSImages.INTRO_BACKGROUND));
			
			//Woman
			_woman = new FlxSprite(350, 404);
			_woman.offset.x = 58;
			_woman.scale.x = _woman.scale.y = 1.5;
			_woman.loadGraphic(OSImages.WOMAN, true, true, 128, 128);
			_woman.addAnimation(IDLE, [45], 0, false);
			_woman.addAnimation(MOVE,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], 20);
			_woman.addAnimation(TAKE_SARI, [45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20], 20, false);
			_woman.play(IDLE);
			add(_woman);
			
			//Monkey
			_monkey = new OSMonkey(FlxG.width + 60, 464, false, 0, 30, true);
			_monkey.animOnly = true;
			_monkey.canCry = false;
			_monkey.facing = FlxObject.RIGHT;
			_monkey.play(OSMonkey.RUN);
			add(_monkey);
			
			//Birds
			add(new OSBird(52, 80, true, false, 20, true));
			add(new OSBird(244, 174, true, false, 20, true));
			add(new OSBird(534, 234, true, false, 20, true));
			add(new OSBird(710, 218, false, false, 20, true));
			add(new OSBird(922, 432, false, false, 20, true));
			_birdTrio = new OSBirdTrio(-50, 20);
			add(_birdTrio);
			
			//Black fade
			_blackFade = new FlxSprite();
			_blackFade.makeGraphic(FlxG.width, FlxG.height, 0xff000000);
			add(_blackFade);
			
			//Blur effects
			/*var effect:OSBlurEffect = new OSBlurEffect(660, 480, 250, 1);
			effect.setXSpeed(-5, 5);
			effect.setYSpeed(-20, -60);
			effect.effectBlobHeight = 10;
			effect.effectBlobWidth = 6;
			effect.effectDisplaceAmtX = 2;
			effect.effectDisplaceAmtY = 3;
			effect.effectAlpha = .6;
			effect.effectLife = 0.5;
			effect.effectBlobCount = 200;
			effect.setStartX(0);
			add(effect);*/
			
			//Credit list
			_creditList = new CreditsList();
			
			_music = new FlxSound();
			_music.loadEmbedded(OSSounds.MUSIC, true);
			_music.volume = 0.5;
			_music.fadeIn(2);
		}
		
		public override function update():void
		{
			super.update();
			_birdTrio.move(FlxG.elapsed * 50, 0);
			
			if(!_cutScene)
			{
				if(FlxG.keys.any())
				{
					_music.fadeOut(2, true);
					FlxG.fade(0xff000000, 2, goBackToMenu);
					_cutScene = true;	
				}
				
				if(!_completeFade)
				{
					_blackFade.alpha -= FlxG.elapsed;
					if(_blackFade.alpha <= 0)
					{
						_completeFade = true;
					}
				}
				else
				{
					if(_monkey.x > 410)
					{
						_monkey.x -= FlxG.elapsed * SPEED * 2.5;
						if(_monkey.x <= 410)
						{
							_monkey.play(OSMonkey.STOP_RUN);
							_woman.play(TAKE_SARI);
							_monkey.x = 410;
						}
					}
					else
					{
						if(!_goHome)
						{
							if(_woman.finished)
							{
								_goHome = true;
								_woman.facing = FlxObject.LEFT;
								_woman.play(MOVE);
								_monkey.play(OSMonkey.START_RUN);
							}
						}
						else 
						{
							if(_woman.x > -150)
							{
								_woman.x -= FlxG.elapsed * SPEED;
								_monkey.x -= FlxG.elapsed * SPEED;
								if(_woman.x <= 150)
								{
									add(_creditList);
								}
							}
							else
							{
								if(!_goToMenu && _creditList.complete)
								{
									_music.fadeOut(2, true);
									_goToMenu = true;
									FlxG.fade(0xff000000, 3, goBackToMenu);
								}
							}
						}
					}
				}
			}
			_music.update();
		}
		
		private function goBackToMenu():void
		{
			FlxG.switchState(new OSMenuState());
		}
		
	}
}
